package com.cpi.basilos.silo.action;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.util.MessageResources;

import com.cpi.basilos.silo.exception.SiloException;
import com.cpi.basilos.silo.form.SiloEntityForm;
import com.cpi.basilos.silo.form.SiloForm;
import com.cpi.basilos.silo.helper.SiloHelper;
import com.cpi.basilos.silo.service.SiloService;
import com.cpi.basilos.silo.view.SiloDatesView;
import com.cpi.basilos.silo.view.SiloFiltersView;
import com.cpi.user.view.UserView;
import com.cs.common.exception.CSException;
import com.cs.common.struts.Actionable;

public class SiloAction extends MappingDispatchAction implements Actionable {
	public ActionForward transits(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws SiloException {
		String forward = null;
		HttpSession httpSession = null;
		MessageResources messageResources = null;
		String messageError = null;
		SiloService siloService = null;
		UserView userView = null;
		List siloEntityViews = null;
		List siloOutputViews = null;
		SiloEntityForm siloEntityForm = null;
		SiloHelper siloHelper = null;
		SiloFiltersView siloFiltersView = null;
		SiloDatesView siloDatesView = null;
		try{
			httpSession = request.getSession();
			siloEntityForm = (SiloEntityForm)form;
			messageResources = getResources(request);
			siloHelper = new SiloHelper();
			siloService =  new SiloService();
			userView = (UserView)httpSession.getAttribute(USER_SESSION_NAME);
			siloDatesView = siloEntityForm.getSiloDatesView();
			if( siloDatesView == null || siloDatesView.getEndDate() == null || siloDatesView.getInitDate() == null ){
				siloDatesView = siloHelper.createDefaultFilter();
				siloEntityForm.setSiloDatesView(siloDatesView);
			}
			siloFiltersView = siloHelper.createFilterToService(siloDatesView, userView);
			siloEntityViews = siloService.getEntities(siloFiltersView);
			siloEntityForm.setSiloEntityViews(siloEntityViews);
			
			siloOutputViews = siloService.getOutputs(siloFiltersView);
			siloEntityForm.setSiloOutputViews(siloOutputViews);
			
			forward = SUCCESS;
		}catch(SiloException siloException){
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), siloException.getErrorCode());
			if(null == messageError){
				httpSession.setAttribute(CUSTOM_EXCEPTION, siloException);
				form.reset(mapping, request);
				throw siloException;
			}else{
				httpSession.setAttribute(ERROR_MESSAGE, siloException);
				forward = FAILS;
			}
		}catch(Exception exception){
			SiloException siloException = null;
			exception.printStackTrace(System.out);
			siloException = new SiloException(exception , CSException.LAYER_ACTION,CSException.ACTION_SELECT);
			form.reset(mapping, request);
			httpSession.setAttribute(CUSTOM_EXCEPTION, siloException);
			throw siloException;
		}
		return mapping.findForward(forward);
	}
	
	public ActionForward saveTransits(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws SiloException {
		
		return mapping.findForward(SUCCESS);
	}
	
	public ActionForward reports(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws SiloException {
		String forward = null;
		HttpSession httpSession = null;
		MessageResources messageResources = null;
		String messageError = null;
		SiloService siloService = null;
		List siloMovementsViews = null;
		SiloForm siloForm = null;
		try{
			httpSession = request.getSession();
			siloForm = (SiloForm)form;
			messageResources = getResources(request);
			siloService =  new SiloService();		
			siloMovementsViews = siloService.getReports(siloForm.getSiloFilterView());
			siloForm.setSiloMovementsViews(siloMovementsViews);
			forward = SUCCESS;
		}catch(SiloException siloException){
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), siloException.getErrorCode());
			if(null == messageError){
				httpSession.setAttribute(CUSTOM_EXCEPTION, siloException);
				form.reset(mapping, request);
				throw siloException;
			}else{
				httpSession.setAttribute(ERROR_MESSAGE, siloException);
				forward = FAILS;
			}
		}catch(Exception exception){
			SiloException siloException = null;
			exception.printStackTrace(System.out);
			siloException = new SiloException(exception , CSException.LAYER_ACTION,CSException.ACTION_LOGIN);
			form.reset(mapping, request);
			httpSession.setAttribute(CUSTOM_EXCEPTION, siloException);
			throw siloException;
		}
		return mapping.findForward(forward);
	}
	
	public ActionForward excelReports(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws SiloException {
		String forward = null;
		HttpSession httpSession = null;
		MessageResources messageResources = null;
		String messageError = null;
		SiloService siloService = null;
		List siloMovementsViews = null;
		SiloForm siloForm = null;
		try{
			httpSession = request.getSession();
			siloForm = (SiloForm)form;
			siloForm.getSiloFilterView().setDate(request.getParameter("dateParam"));
			messageResources = getResources(request);
			siloService =  new SiloService();
			siloMovementsViews = siloService.getReports(siloForm.getSiloFilterView());
			siloForm.setSiloMovementsViews(siloMovementsViews);
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-Disposition", "attachment; filename=SiloReports.xls");
			forward = SUCCESS;
		}catch(SiloException siloException){
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), siloException.getErrorCode());
			if(null == messageError){
				httpSession.setAttribute(CUSTOM_EXCEPTION, siloException);
				form.reset(mapping, request);
				throw siloException;
			}else{
				httpSession.setAttribute(ERROR_MESSAGE, siloException);
				forward = FAILS;
			}
		}catch(Exception exception){
			SiloException siloException = null;
			exception.printStackTrace(System.out);
			siloException = new SiloException(exception , CSException.LAYER_ACTION,CSException.ACTION_LOGIN);
			form.reset(mapping, request);
			httpSession.setAttribute(CUSTOM_EXCEPTION, siloException);
			throw siloException;
		}
		
		return mapping.findForward(forward);
	}
	
}